How to stop getting prompted to "Confirm"

Hello,

I have some Windows Server 2008 systems that I'm trying to run a powershell script on to delete some temp files, but I keep getting prompted with "Confirm... Y [Yes] [A] Yes to All...... ect"  Is there a way to bypass the Confirm?

Thanks,

Tom

July 28th, 2011 12:57am

If you are using Remove-Item cmdlet use confirm switch like in below example

Remove-Item .\v.txt -Confirm:$false

Free Windows Admin Tool Kit Click here and download it now
July 28th, 2011 1:04am

Hi Krystian,

I'm using the below command typed directly into PowerShell just for testing purposes.  So, I should just be able to add -Confirm:$false to the end and it should not prompt me to Confirm? 

get-childitem C:\inetpub\logs\LogFiles -recurse | where-object { $_.LastAccessTime -lt (Get-Date).AddDays(-14) -and $_.LastWriteTime -lt (Get-Date).AddDays(-14) } | remove-item -ErrorAction SilentlyContinue

Thanks,

Tom

July 28th, 2011 1:16am

Yes it should work by just appending -Confirm:$false at the end
Free Windows Admin Tool Kit Click here and download it now
July 28th, 2011 2:24am

Yes, if you set $confirm to false explicitly, it shouldn't prompt you to confirm.

 

get-childitem C:\inetpub\logs\LogFiles -recurse | where-object { $_.LastAccessTime -lt (Get-Date).AddDays(-14) -and $_.LastWriteTime -lt (Get-Date).AddDays(-14) } | remove-item -ErrorAction SilentlyContinue -Confirm:$false

 

Alternatively, you can try -force parameter as well.

get-childitem C:\inetpub\logs\LogFiles -recurse | where-object { $_.LastAccessTime -lt (Get-Date).AddDays(-14) -and $_.LastWriteTime -lt (Get-Date).AddDays(-14) } | remove-item -ErrorAction SilentlyContinue -Force

 

PS: Test above changes before trying out in production environment.

July 28th, 2011 4:36am

I just added -Confirm:$false to the end, but when I run the command locally on the server I'm still getting the following:

Confirm

The item at

Microsoft.Powershell.Core\FileSystem::C:\inetpub\logs\LogFiles\W3SVC1 has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue?

[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help

(default is “Y”):

 

I then tried it with –Force, but I received the same Confirm prompt. 

 

Free Windows Admin Tool Kit Click here and download it now
July 28th, 2011 6:26pm

Looking at the command you're running.. it will remove both files and folders. If it finds an old folder, it will attempt to remove it (and its contents) regardless if the files within it has been recently accessed or modified. Is this the intent? 
July 28th, 2011 8:05pm

Yes, it should remove anything older than 14 days.... files or folders / folders and files.

Thanks,

Tom

Free Windows Admin Tool Kit Click here and download it now
July 28th, 2011 9:45pm

Do you mind posting your code here for better clarity? I generally delete all temp files in c:\windows\temp with below code. I face no Confirm prompts.

$TempFiles = Get-ChildItem c:\windows\temp -include *.tmp -recurse

$TempFiles | foreach ($_) { remove-item $_.Fullname -force}

 

July 29th, 2011 4:38am

Yes, it should remove anything older than 14 days.... files or folders / folders and files.

Thanks,

Free Windows Admin Tool Kit Click here and download it now
July 29th, 2011 5:17am

Here’s the code we are using to delete anything older than 14 days.  Any suggestions is much appreciated. Thanks

get-childitem C:\inetpub\logs\LogFiles -recurse | where-object { $_.LastAccessTime -lt (Get-Date).AddDays(-14) -and $_.LastWriteTime -lt (Get-Date).AddDays(-14) } | remove-item -ErrorAction SilentlyContinue -Confirm:$false

 

 

July 29th, 2011 4:19pm

This has probably already been answered but I was running into the same problem as you and couldn't find an answer.

Maybe if I post it here I will be able to find it the next time I run into it. :-)

I think this will work for you

get-childitem C:\inetpub\logs\LogFiles -include *.log  -recurse | where-object { $_.LastAccessTime -lt (Get-Date).AddDays(-14) -and $_.LastWriteTime -lt (Get-Date).AddDays(-14) } | remove-item -ErrorAction SilentlyContinue -Confirm:$false

replace *.log without ever wildcard you need.

I was executing this

get-childitem  -Recurse  c:\logs\ | where {$_.LastWriteTime -le $now.AddDays(-30)} | del -whatif

and getting the same as you

Confirm

The item at

Microsoft.Powershell.Core\FileSystem::C:\logs\blah has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue?

I replaced it with

get-childitem  -Recurse  c:\logs\ -include *log*| where {$_.LastWriteTime -le $now.AddDays(-30)} | del -whatif

and like magic no more prompts.


Free Windows Admin Tool Kit Click here and download it now
May 31st, 2012 8:37pm

Hello Folks,

I'm having this issue with the "Confirm" dialog continuously popping up, no matter what combination of parameters I try.  I've been reading this blog and tried using multiple combinations, to no avail.

Here is my command: Get-ChildItem 'c:\builds\testing' -recurse | where {$_.LastWriteTime -le (Get-Date).AddDays(0)} | del

-ErrorAction SilentlyContinue -Confirm:$false

If any one can help it would be greatly appreciated.

NOTE:  I also set the $ConfirmPreference = "None"

Thanks In Advance!

June 19th, 2012 7:05pm

Hello Folks,

I was able to solve this probably, with some internal help.

Here is my command I used to suppress the confirm dialog that I was getting:

Get-ChildItem c:\builds\testing -recurse | where-object {$_.LastWriteTime -le (Get-Date).AddDays(0)} | remove-item -force

-recurse

I hope this will be of some help to someone else that is having the same problem.

Thanks,

Free Windows Admin Tool Kit Click here and download it now
June 19th, 2012 8:54pm

Hello,

Does anybody know how can I add exclusions by folder(and all its contents) to this script ?

I have 3-4 folders and their content which I would like to keep!

thanks!

June 9th, 2013 3:42pm

Hi,

You can use the existing where clause to exclude the folders. If you still have questions, you're probably better off starting a new thread, as this one is old and already marked as answered.

Free Windows Admin Tool Kit Click here and download it now
June 9th, 2013 8:19pm

Thanks Willie J. Pee... this was my solution!
January 30th, 2014 2:22pm

Have been there have strugled with that ... but     -comfirm:0  has worked for me . also using  -verbose but that should be a different matter.   you could try $false= 0 , in Your site constant and variable section... 


Free Windows Admin Tool Kit Click here and download it now
June 3rd, 2015 4:19am

Have been there have strugled with that ... but     -comfirm:0  has worked for me . also using  -verbose but that should be a different matter.   you could try $false= 0 , in Your site constant and variable section... 


June 3rd, 2015 8:15am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics